package com.sk.bilibili.model.activity;

import android.database.DatabaseUtils;
import android.text.SpannableStringBuilder;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.MemberMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.DataUtils;
import com.sk.bilibili.utils.PreferenceUtils;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.utils.StringUtils;
import com.sk.bilibili.view.activity.LoginActivity;

public class LoginModel extends BaseModel<LoginActivity> {

    public SpannableStringBuilder getText(int color) {
        return StringUtils.getColorText("登录即代表你同意用户协议和隐式政策", color, 8, 12, 13, 17);
    }

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    PreferenceUtils preferenceUtils;

    public void login(String phone, String password) {
        JsonMap map=new JsonMap();
        map.put("member.phone",phone);
        map.put("member.password",password);
        RequestUtils.process(memberMapper.login(map), new RequestCallBack<Member>() {
            @Override
            public void success(Member member) {
                preferenceUtils.setString("phone",phone);
                preferenceUtils.setString("password",password);
                preferenceUtils.setString("bSessionId",member.getExtra().getBSessionId());
                DataUtils.setMemberId(member.getId());
                view.loginSuccess();
            }
            @Override
            public void error(Throwable throwable) {
                view.loginError();
            }
        });
    }

}